package com.sir.tao.carhire.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sir.tao.carhire.entity.table.AdministratorsEntity;
import com.sir.tao.carhire.common.CommonResult;
import com.sir.tao.carhire.entity.table.AdministratorsRoleRelationEntity;
import com.sir.tao.carhire.entity.vo.AdministratorsVo;
import com.sir.tao.carhire.service.AdministratorsRoleRelationService;
import com.sir.tao.carhire.service.AdministratorsService;
import com.sir.tao.carhire.shiro.JwtUtil;
import com.sir.tao.carhire.util.RedisUtil;
import com.sir.tao.carhire.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * @author tao
 * @data 2021/12/6 10:48
 * @description: 管理员登录接口
 */
@RestController
@Api(tags = "管理员登录接口")
@RequestMapping("/administrators")
public class AdministratorsController {


    @Resource
    private AdministratorsService administratorsService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private AdministratorsRoleRelationService administratorsRoleRelationService;


    /**
     * 新增一个管理员
     * @param administratorsVo 管理员实体
     * @return 新增一个管理员
     */
    @RequiresRoles("超级管理员")
    @ApiOperation("新增一个管理员")
    @ApiImplicitParam(name = "新增一个管理员", value = "新增一个管理员")
    @PostMapping("/addUser")
    public CommonResult<?> addUser(@RequestBody AdministratorsVo administratorsVo){
        AdministratorsEntity administratorsEntity1 = administratorsService.getOne(new LambdaQueryWrapper<AdministratorsEntity>()
                .eq(AdministratorsEntity::getUsername,administratorsVo.getUsername())
        );
        if (Objects.nonNull(administratorsEntity1)){
            return CommonResult.fail("该用户名已经存在");
        }
        AdministratorsEntity administratorsEntity = voToEntity(administratorsVo);
        boolean i = administratorsService.save(administratorsEntity);
        if(i){
            AdministratorsEntity administratorsEntity2 = administratorsService.getOne(new LambdaQueryWrapper<AdministratorsEntity>()
                    .eq(AdministratorsEntity::getUsername,administratorsEntity.getUsername())
            );

            List<AdministratorsRoleRelationEntity> administratorsRoleRelationEntities = administratorsVo.getRoles().stream()
                    .map(s -> {
                        AdministratorsRoleRelationEntity administratorsRoleRelationEntity = new AdministratorsRoleRelationEntity();
                        administratorsRoleRelationEntity.setAdministratorsId(administratorsEntity2.getId());
                        administratorsRoleRelationEntity.setRoleId(s);
                        return administratorsRoleRelationEntity;
                    }).collect(Collectors.toList());
            //为该管理员添加角色
            administratorsRoleRelationService.saveBatch(administratorsRoleRelationEntities);

            return CommonResult.success("添加成功");
        }else {
            return CommonResult.fail("添加失败");
        }
    }

    private AdministratorsEntity voToEntity(AdministratorsVo administratorsVo) {
        AdministratorsEntity administratorsEntity = new AdministratorsEntity();
        administratorsEntity.setUsername(administratorsVo.getUsername());
        administratorsEntity.setPassword(administratorsVo.getPassword());
        administratorsEntity.setPhone(administratorsVo.getPhone());
        return administratorsEntity;
    }

    /**
     * 管理员登录接口
     * @param administratorsEntity 传的账号密码
     * @return 成功返回token
     */
    @PostMapping("/login")
    @ApiOperation("管理员登录接口")
    @ApiImplicitParam(name = "管理员登录接口", value = "管理员登录接口")
    public CommonResult<?> login(@RequestBody AdministratorsEntity administratorsEntity){

        if(StringUtils.isEmpty(administratorsEntity.getUsername()) || StringUtils.isEmpty(administratorsEntity.getPassword())){
            return CommonResult.fail("账号密码不能为空");
        }
        boolean b = administratorsService.login(administratorsEntity);
        if(b){
            //登录成功
            String token = (String) redisUtil.get("prefix_user_token_"+administratorsEntity.getUsername());
            return CommonResult.success(token);
        }else {
            //登录失败
            return CommonResult.fail("账号或密码错误");
        }
    }
    /**
     * 管理员退出接口
     * @param request 请求
     * @return 结果
     */
    @GetMapping("/loginOut")
    @ApiOperation("管理员退出接口")
    @ApiImplicitParam(name = "管理员退出接口", value = "管理员退出接口")
    public CommonResult<?> loginOut(HttpServletRequest request){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        String token = request.getHeader("Authorization");
        String userName = JwtUtil.getUserName(token);
        redisUtil.del("prefix_user_token_"+userName);
        redisUtil.del("prefix_user_entity_"+userName);
        return CommonResult.fail("成功退出");
    }
    @RequiresRoles("超级管理员")
    @GetMapping("test")
    public String test(){
        return "成功";
    }

}
